package py.com.alfa.escriba.web.services.impl;

import java.util.List;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;

import py.com.alfa.escriba.exception.ErrorCode;
import py.com.alfa.escriba.exception.ServiceException;
import py.com.alfa.escriba.persistence.dao.PersonasdocumentosDAO;
import py.com.alfa.escriba.persistence.dto.PersonasdocumentosDTO;
import py.com.alfa.escriba.util.Utils;
import py.com.alfa.escriba.web.interceptor.InvocationContext;
import py.com.alfa.escriba.web.services.PersonasdocumentosService;

public class PersonasdocumentosServiceImpl implements PersonasdocumentosService{

  private PersonasdocumentosDAO personasdocumentosDAO;

  public void setPersonasdocumentosDAO(PersonasdocumentosDAO personasdocumentosDAO) {
    this.personasdocumentosDAO = personasdocumentosDAO;
  }

  @Override
  public List<PersonasdocumentosDTO> listPersonasdocumentos(InvocationContext ic) throws ServiceException {
    List<PersonasdocumentosDTO> personasdocumentoss =  personasdocumentosDAO.listPersonasdocumentos();

    return personasdocumentoss;
  }

  @Override
  public PersonasdocumentosDTO getPersonasdocumentos(Long id, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    PersonasdocumentosDTO personasdocumentos =  personasdocumentosDAO.getPersonasdocumentosById(id);

    if (personasdocumentos == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"personasdocumentos.notfound", id);

    return personasdocumentos;
  }

  @Override
  public void deletePersonasdocumentos(Long id, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    PersonasdocumentosDTO personasdocumentos =  personasdocumentosDAO.getPersonasdocumentosById(id);

    if(personasdocumentos == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"personasdocumentos.notfound", id);

    try {
      personasdocumentosDAO.deletePersonasdocumentos(personasdocumentos);
    }
    catch (DataIntegrityViolationException e){
      throw new ServiceException(ErrorCode.DATA_INTEGRITY_VIOLATION, "personasdocumentos.constraint.violation", id);
    }

  }

  @Override
  public PersonasdocumentosDTO updatePersonasdocumentos(Long id, PersonasdocumentosDTO dto, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    if (null == dto)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","dto");

    PersonasdocumentosDTO personasdocumentos = personasdocumentosDAO.getPersonasdocumentosById(id);

    if (personasdocumentos == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"personasdocumentos.notfound", id);

    personasdocumentos.setIdpersona(dto.getIdpersona());
    personasdocumentos.setIdtipodocumento(dto.getIdtipodocumento());
    personasdocumentos.setNrodocumento(dto.getNrodocumento());
    personasdocumentos.setIsprincipal(dto.getIsprincipal());
    personasdocumentos.setPais(dto.getPais());
    personasdocumentos.setVencimiento(dto.getVencimiento());
    personasdocumentos.setActivo(dto.getActivo());
    personasdocumentos.setDvdocumento(dto.getDvdocumento());
 

    Utils.validarDTO(dto);

    personasdocumentosDAO.updatePersonasdocumentos(dto);

    return personasdocumentos;
  }

  @Override
  public PersonasdocumentosDTO createPersonasdocumentos(PersonasdocumentosDTO dto, InvocationContext ic) throws ServiceException {
    if (dto == null)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","dto");

    try {

      Utils.validarDTO(dto);

      personasdocumentosDAO.createPersonasdocumentos(dto);
    }
    catch (DuplicateKeyException e){
      throw new ServiceException(ErrorCode.DUPLICATE_KEY_EXCEPTION, "personasdocumentos.duplicate.key");
    }

    return dto;
  }

}